package indi.zhifa.engine.bailan6.common.web.config.web;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@ConditionalOnProperty(name = "spring.profiles.active",havingValue="prod")
@Configuration
public class CorsConfig {
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("*") // 设置允许访问的源，可以设置多个，如："http://example.com", "http://example.org"
                        .allowedMethods("GET", "POST", "PUT", "DELETE") // 设置允许的HTTP方法
                        .allowedHeaders("*") // 设置允许的请求头
                        .allowCredentials(true) // 设置是否允许发送Cookie
                        .maxAge(3600); // 设置缓存时间，单位秒
            }
        };
    }
}
